package com.example.warehousems.event.publisher;

import com.example.warehousems.dao.model.Storage;
import com.example.warehousems.event.event.InOutEvent;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationEventPublisher;
import org.springframework.stereotype.Component;

@Component
public class InOutEventPublisher {
    @Autowired
    private final ApplicationEventPublisher applicationEventPublisher;

    public InOutEventPublisher(ApplicationEventPublisher applicationEventPublisher) {
        this.applicationEventPublisher = applicationEventPublisher;
    }

    public Storage publish(Storage storage, Integer id) { // id:1-入库 2-出库
        String message = "";
        if (id == 1) {
            message = storage.getCargo().getId() + "完成入库。  位置："
                    + storage.getShelf() + "-" + storage.getFloor() + "-" + storage.getNumber();
//            storage.setStatus(1);
//            storage.getCargo().setStatus(1);
        } else if (id == 2) {
            message = storage.getCargo().getId() + "完成出库。  位置："
                    + storage.getShelf() + "-" + storage.getFloor() + "-" + storage.getNumber()
                    + "  所运往分支仓库：" + storage.getCargo().getOutBranch().getCity();
            storage.setStatus(0);
            storage.getCargo().setStatus(2);
        }
        InOutEvent event = new InOutEvent(storage, message, id);
        applicationEventPublisher.publishEvent(event);
        return storage;
    }
}
